package ee.roosideSoda.frontend.controller;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ee.roosideSoda.GeneralConstants;
import ee.roosideSoda.backend.model.dao.factory.DAOFactory;
import ee.roosideSoda.frontend.controller.menucontrollers.MenuController;
import ee.roosideSoda.frontend.controller.menucontrollers.MenuControllerFactory;
import ee.roosideSoda.frontend.view.ViewManager;

/**
 * Servlet implementation class FrontController
 */
@WebServlet("/FrontController")
public class FrontController extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(FrontController.class);
    DAOFactory daoFactory = DAOFactory.getDAOFactory();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FrontController() 
    {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		request.setCharacterEncoding(GeneralConstants.ENCODING_ISO_8859_1);
		response.setContentType(GeneralConstants.CONTENT_TYPE_HTML_ISO_8859_1);
	
		try
		{
			ViewManager viewManager = new ViewManager();
			String view = "mainMenu";
			ServletContext servletContext = getServletConfig().getServletContext();
		
			MenuControllerFactory menuControllerFactory  = new MenuControllerFactory();
			MenuController menuController = null;
			
			menuController = menuControllerFactory.create(request, response);
			if (menuController != null && !menuController.equals(""))
			{
				view = menuController.service(request, response);
			}
		
			viewManager.navigate(view, request, response, servletContext);	
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}

/* Code for printing out all request.getParameter and request.getAttributes arguments
System.out.println("To out-put All the request-attributes received from request - ");
Enumeration<String> enAttr = request.getAttributeNames(); 
while(enAttr.hasMoreElements()){
 String attributeName = (String)enAttr.nextElement();
 logger.warn("Attribute Name - "+attributeName+", Value - "+(request.getAttribute(attributeName)).toString());
}

System.out.println("To out-put All the request parameters received from request - ");

Enumeration<String> enParams = request.getParameterNames(); 
while(enParams.hasMoreElements()){
 String paramName = (String)enParams.nextElement();
 logger.warn("Attribute Name - "+paramName+", Value - "+request.getParameter(paramName));
}
*/
